﻿<cfscript>
/** 
* ORM TeacherBook
* 
* @hint "Hibernate persistenter t_teacher_book. 学期教师参考用书目录" 
* 
*/ 

component 
	displayname="public.service.orm.senate.TeacherBook" 
	output=false 
	persistent=true 
	selectBeforeUpdate=true dynamicInsert=true dynamicUpdate=true 
	cacheuse="Transactional" 
	entityname="TeacherBook" 
	table="t_teacher_book"
	{
	
	property type="string" name="TeacherBookID" column="tbid" fieldtype="id" generator="assigned" hint="教参计划号";

	property type="string" name="BookIndex" column="bidx" hint="学期教参序列";

	property type="numeric" name="Prise" column="prise" hint="定价";
	property type="numeric" name="Discount" column="discount" hint="折扣率";
	property type="numeric" name="Charge" column="charge" hint="实际售价";
	property type="numeric" name="PlanCounts" column="plan_sum" hint="采购数量";

	property type="string" name="Contract" column="contract" hint="联系人";
	property type="string" name="Telephone" column="telephone" hint="联系电话";

	property name="Term" fieldtype="many-to-one" fkcolumn="term_id" cfc="Term"  hint="学期";
	property name="Teacher" fieldtype="many-to-one" fkcolumn="tch_id" cfc="Teacher"  hint="教师";
	property name="Book" fieldtype="many-to-one" fkcolumn="bid" cfc="Book"  hint="教材";
	
	/**
	* @hint "生成课程教材序列"
	* 
	* @Term "学期"
	*/
	public string function getBookIndex( required Term ) output=false {
		/* 
			课程教材序列规则 4位代码
		*/
		var nextID = numberFormat(1, "0___");
		
		var checkArray = ORMExecuteQuery("select BookIndex from TeacherBook 
											where 
											Term = :term 
											order by BookIndex desc", {term=arguments.Term}, { maxResults=1 });
		
		if (arrayLen(checkArray)) {
			/* 创建一个追加id */
			nextID = numberFormat((checkArray[1] + 1), "0___");
		}

		return nextID;
	}
	
	/**
	* @hint "自定义主键规则"
	* 
	* @Term "学期"
	* @BookIndex "学期教参序列"
	*/
	public string function getID( required Term, required string BookIndex ) output=false {
		
		var nextID = arguments.Term.getTermID() & "," &  arguments.BookIndex;

		/* 
			主键生成规则 
			
			教参计划号8位 3位学期代码, 4位教材序列

			Unique: TERM_ID, BIDX
		*/
		return nextID;
	}
	
}

</cfscript>